import { addUserTrainingPlanRelationUsingPost } from '@/services/backend/userTrainingPlanRelationController';
import { ProColumns, ProSchemaValueEnumMap, ProTable } from '@ant-design/pro-components';
import '@umijs/max';
import { message, Modal } from 'antd';
import React from 'react';

interface Props {
  modalVisible: boolean;
  userEnumMap: ProSchemaValueEnumMap;
  planEnumMap: ProSchemaValueEnumMap;
  columns: ProColumns<API.UserTrainingPlanRelation>[];
  onSubmit: () => void;
  onCancel: () => void;
}

/**
 * @en-US Add node
 * @zh-CN 添加节点
 * @param fields
 */
const handleAdd = async (fields: API.UserTrainingPlanRelationAddRequest) => {
  const hide = message.loading('正在添加');
  try {
    await addUserTrainingPlanRelationUsingPost({
      ...fields,
    });
    hide();
    message.success('创建成功');
    return true;
  } catch (error: any) {
    hide();
    message.error(`创建失败,${error.message}`);
    return false;
  }
};

const CreateModal: React.FC<Props> = (props) => {
  const { modalVisible,userEnumMap,planEnumMap, columns, onSubmit, onCancel } = props;
  console.log(columns)

  const specialColumns: ProColumns<API.UserTrainingPlanRelationVO>[] = [
    {
      title: 'id',
      dataIndex: 'id',
      valueType: 'text',
      hideInForm: true,
    },
    {
      title: '用户姓名',
      dataIndex: 'userId',
      valueEnum: userEnumMap,
      hideInSearch: true,
    },
    {
      title: '训练名称',
      dataIndex: 'planId',
      valueEnum: planEnumMap,
      hideInSearch: true,
    },
    {
      title: '状态',
      dataIndex: 'status',
      valueType: 'select',
      valueEnum: {
        active: { text: '正在进行', status: 'ongoing' },
        archived: { text: '已完成', status: 'completed' },
      },
    },
    {
      title: '创建时间',
      sorter: true,
      dataIndex: 'createTime',
      valueType: 'dateTime',
      hideInSearch: true,
      hideInForm: true,
    },
    {
      title: '更新时间',
      sorter: true,
      dataIndex: 'updateTime',
      valueType: 'dateTime',
      hideInSearch: true,
      hideInForm: true,
    }
  ];

  return (
    <Modal title={'新建'} open={modalVisible} destroyOnClose footer={null} onCancel={onCancel}>
      <ProTable
        type={'form'}
        columns={specialColumns}
        onSubmit={async (value) => {
          const success = await handleAdd(value);
          if (success) {
            onSubmit?.();
          }
        }}
      />
    </Modal>
  );
};
export default CreateModal;
